package nl.uva.science.luna.pico.compiler;

import java.util.Hashtable;
import nl.uva.science.luna.pico.language.types.PICOID;

/**
 * @author Sander Vellinga
 *
 * This class is used to store variable locations of pico programs
 */
public class SymbolTable {
	private Hashtable<PICOID, Integer> m_hashtable;
	private Integer m_location;
	
	/**
	 * The constructor of this class
	 */
	public SymbolTable() {
		m_hashtable = new Hashtable<PICOID, Integer>();
		m_location = 1;
	}
	
	/**
	 * This method stores a variable and sets a location pointer to this variable
	 * 
	 * @param key
	 * @return location
	 */
	public Integer put(PICOID key) {
		int varLocation = -1;
		if (m_hashtable.containsKey(key) == false) {
			m_hashtable.put(key, m_location);
			m_location++;
			
			// Get the location of the stored variable so we can return this
			varLocation = m_hashtable.get(key);
		}
		return varLocation;
	}
	
	/**
	 * This method retrieves the location pointer of an given variable
	 * 
	 * @param key
	 * @return location
	 */
	public Integer get(PICOID key) {
		if (m_hashtable.containsKey(key) == true) {
			return m_hashtable.get(key);
		} else {
			return 0;
		}
	}
}
